from sklearn.datasets import load_breast_cancer
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import xgboost as xgb

dataset = load_breast_cancer()

# 提取特征数据和目标数据
X = dataset.data
y = dataset.target

# 将数据集以9:1的比例随机分为训练集和测试集，为了重现随机分配设置随机种子，即random_state参数
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=188)

# 定义模型参数
params = {
    "max_depth": 3,
    "objective": "binary:logistic",
    "eval_metric": "logloss",
}

# 将数据转换为XGBoost的特征矩阵和标签格式
dtrain = xgb.DMatrix(X_train, label=y_train)

dtest = xgb.DMatrix(X_test, label=y_test)

# 训练模型
model = xgb.train(params, dtrain, num_boost_round=10)

# 预测测试集的结果
y_pred = model.predict(dtest)

# 将预测的概率转换为类别标签
y_pred = [1 if p > 0.5 else 0 for p in y_pred]

# 计算模型在测试集上的准确率
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))